package service

import (
	"context"

	"algo-sdk-go/project/matchmaking/application/command"
	"algo-sdk-go/project/matchmaking/application/dto"
	"algo-sdk-go/project/matchmaking/domain/models/entity"
	domainService "algo-sdk-go/project/matchmaking/domain/service"
)

type MatePreferenceService struct {
	matchMakingDomain *domainService.MatchMakingDomain
}

func NewMatePreferenceService(matchMakingDomain *domainService.MatchMakingDomain) *MatePreferenceService {
	return &MatePreferenceService{
		matchMakingDomain: matchMakingDomain,
	}
}

func (m *MatePreferenceService) GetMatePreference(ctx context.Context, userNumList []int) ([]entity.MatePreference, error) {
	return m.matchMakingDomain.LoadMatePreference(ctx, userNumList)
}

func (m *MatePreferenceService) BatchAddMatePreference(ctx context.Context, cmdList []command.MatePreferenceCommand) (dto.SyncResult, error) {
	var (
		idList     []int
		failedList []dto.FailedReason
	)
	for _, preferenceCommand := range cmdList {
		id, err := m.matchMakingDomain.SyncMatePreference(ctx, preferenceCommand)
		if err != nil {
			failedList = append(failedList, dto.FailedReason{
				UserNum: preferenceCommand.UserNum,
				Reason:  err.Error(),
			})
			continue
		}
		idList = append(idList, id)
	}
	return dto.SyncResult{
		SuccessList: idList,
		FailedList:  failedList,
	}, nil
}
